package ipmsg.file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;










public class ConfigOperation
{
  private Properties config = new Properties();
  private String fn = null;
  

  public ConfigOperation() {}
  
  public ConfigOperation(String fileName)
    throws ConfigurationException
  {
    try
    {
      FileInputStream fin = new FileInputStream(fileName);
      this.config.load(fin);
      
      fin.close();
    } catch (IOException ex) {
      throw new ConfigurationException("无法读取指定的配置文件:" + fileName);
    }
    
    this.fn = fileName;
  }
  
  public String getValue(String itemName)
  {
    return this.config.getProperty(itemName);
  }
  






  public void setValue(String itemName, String value)
  {
    this.config.setProperty(itemName, value);
  }
  
  public void saveFile(String fileName, String description)
    throws ConfigurationException
  {
    try
    {
      FileOutputStream fout = new FileOutputStream(fileName);
      this.config.store(fout, description);
      
      fout.close();
    } catch (IOException e) {
      throw new ConfigurationException("无法保存指定的配置文件:" + fileName);
    }
  }
  

  public void saveFile(String fileName)
    throws ConfigurationException
  {
    saveFile(fileName, "");
  }
  
  public void saveFile() throws ConfigurationException
  {
    if (this.fn.length() == 0) {
      throw new ConfigurationException("需指定保存的配置文件名");
    }
    saveFile(this.fn);
  }
}
